#include <algorithm>
#include <stdexcept>

#include "DemoContainer.h"
#include "DemoContainerCommon.h"

namespace democontainer
{

  DemoContainer::DemoContainer()
  {
    Container_.clear();
  }

  DemoContainer::~DemoContainer()
  {
  }

  int DemoContainer::get_size( void )
  {
    int ret = 0;

    ret = Container_.size();

    return ret;
  }

  int DemoContainer::add_item( const std::string& str )
  {
    int ret = -1;
    bool ret_find = false;

    for ( common::Container::size_type i = 0; i < Container_.size(); i++ )
    {
      if ( Container_[i] == str )
      {
        ret_find = true;
        break;
      }
    }

    if ( !ret_find )
    {
      Container_.push_back(str);
      ret = Container_.size() - 1;
    }

    return ret;
  }

  std::string DemoContainer::get_item( int index )
  {
    std::string ret;

    if ( ( index < 0 ) 
      || ( ((common::Container::size_type)index) >= Container_.size() ) )
    {
      throw std::out_of_range( "The index is error!" );
    }
   
    ret = Container_.at(index);

    return ret;
  }

  bool DemoContainer::remove_item( int index )
  {
    bool ret = false;

    if ( ( index >= 0 ) 
      && ( ((common::Container::size_type)index) < Container_.size() ) )
    {
      common::Container::iterator iter =
        std::find(Container_.begin(), Container_.end(), Container_[index]);

      if ( iter != Container_.end() )
      {
        Container_.erase(iter);
        ret = true;
      }
    }

    return ret;
  }

};